package com.travel.controller.admin;

import cn.hutool.core.bean.BeanUtil;
import com.travel.common.result.Result;
import com.travel.pojo.dto.PageDTO;
import com.travel.pojo.dto.ScenicSpotDTO;
import com.travel.pojo.dto.ScenicSpotQueryDTO;
import com.travel.pojo.entity.ScenicSpot;
import com.travel.pojo.vo.ScenicSpotVO;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

@RestController
@Slf4j
@RequiredArgsConstructor
@CrossOrigin
@RequestMapping("/api/scenic-spots")
public class ScenicSpotController {

    private final IScenicSpotService scenicSpotService;

    @GetMapping("/{id}")
    public Result<ScenicSpotVO> getScenicSpotById(@PathVariable Long id) {
        ScenicSpot scenicSpot = scenicSpotService.getById(id);
        if (scenicSpot == null) {
            return Result.error("景区不存在");
        }
        return Result.success(BeanUtil.copyProperties(scenicSpot, ScenicSpotVO.class));
    }

    @PostMapping("/list")
    public Result<PageDTO<ScenicSpotVO>> pageListScenicSpot(@RequestBody(required = false) ScenicSpotQueryDTO scenicSpotQueryDTO) {
        if (scenicSpotQueryDTO == null) {
            scenicSpotQueryDTO = new ScenicSpotQueryDTO(); // 提供默认值
        }

        // 设置默认分页值
        if (scenicSpotQueryDTO.getPage() == null) {
            scenicSpotQueryDTO.setPage(1);
        }
        if (scenicSpotQueryDTO.getPageSize() == null) {
            scenicSpotQueryDTO.setPageSize(10);
        }

        log.info("搜索参数为：{}", scenicSpotQueryDTO);
        return Result.success(scenicSpotService.pageListScenicSpot(scenicSpotQueryDTO));
    }

    @PostMapping
    public Result<Void> createScenicSpot(@RequestBody ScenicSpotDTO scenicSpotDTO) {
        scenicSpotService.createScenicSpots(scenicSpotDTO);
        return Result.success();
    }

    @PutMapping("/{id}")
    public Result<Void> updateScenicSpot(@PathVariable Long id, @RequestBody ScenicSpotDTO scenicSpotDTO) {
        scenicSpotService.updateScenicSpot(id, scenicSpotDTO);
        return Result.success();
    }

    @DeleteMapping("/{id}")
    public Result<Void> deleteScenicSpot(@PathVariable Long id) {
        scenicSpotService.removeById(id);
        return Result.success();
    }
}
